package com.itema.controller;

import com.github.pagehelper.Page;
import com.itema.pojo.Clazz;
import com.itema.pojo.ClazzQueryParam;
import com.itema.pojo.PageResult;
import com.itema.pojo.Result;
import com.itema.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    ClazzService clazzService;

    /**
     * 2.1 条件分页查询接口
     */
    @GetMapping
    public Result page(ClazzQueryParam clazzQueryParam) {
        PageResult<Clazz> pageResult = clazzService.list(clazzQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 添加班级信息
     */
    @PostMapping
    public Result add(@RequestBody Clazz clazz) {
        log.info("添加班级信息: {}", clazz);
        clazzService.add(clazz);
        return Result.success();
    }

    /**
     * 根据Id查询
     */
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id){
        log.info("根据Id查询:{}", id);
        Clazz clazz=clazzService.findById(id);
        return Result.success(clazz);
    }

    /**
     * 修改班级信息
     */
    @PutMapping
public Result update(@RequestBody Clazz clazz){
        log.info("修改班级信息: {}", clazz);
        clazzService.update(clazz);
        return Result.success();
    }

    /**
     * 删除班级信息
     */
    @DeleteMapping("/{id}")
    public Result delete( @PathVariable Integer id){
        log.info("删除班级信息:{}", id);//占位符替换
        clazzService.deleteById(id);
        return Result.success();
}

/**
 * 查询所有班级
 */
@GetMapping("/list")
    public Result list(){
        log.info("查询所有班级");
        List<Clazz> clazzList=clazzService.findAll();
        return Result.success(clazzList);

}}
